import { searchAll } from '../../../../api/navigation';
import { findByPCateId } from '../../../../api/base'

Component({
  properties: {
    condition: {
      type: Object,
      value: {}
    },
    listData: {
      type: Array,
      value: []
    },
    listHeight: {
      type: Number,
      value: 0
    }
  },
  data: {
    loading: false,
    searchCondition: {
      keyWord: '',
      navTagsId: '',
      searchBizTypeEnum: 'WEB_CAST',
      filterMap: {}
    },
    filterMap: {},
    isLiveEnd: false,
    isNoLiveData: false,
    list: [],
    livePager: {
      page: 1,
      pageSize: 10
    },
    
    popupTop: 0,
    showCondition: false,
    menuTitle: '',
    menuActiveIndex: 0,
    menus: [],
    menuActive: '',
    options: [],
  },
  observers: {
    'condition': function(val) {
      const _this = this
      if (val.keyword) {
        // 设置条件
        _this.setData({
          'searchCondition.keyWord': !val.keyword?'':val.keyword,
          'searchCondition.navTagsId': !val.navTagsId?'':val.navTagsId,
        })
        if (val.selectCondition && val.selectCondition.length) {
          val.selectCondition.forEach((v, index) => {
            let filterMap = this.data.filterMap
            filterMap[v.name] = ''
            _this.setData({
              menuTitle: v.label,
              filterMap: filterMap
            })
            _this.getMenus(v.id)
          })
        }
        _this.resetData()
      }
    }
  },
  options: {
    styleIsolation: 'shared'
  },
  methods: {
    search() {
      if (!this.data.isLiveEnd) {
        this.setData({ loading: true })
        searchAll({
          condition: this.data.searchCondition,
          pager: this.data.livePager
        }).then(res => {
          this.setData({ loading: false })
          let list = this.data.list;
          if (res.page && res.page.records.length) {
            list = list.concat(res.page.records);
            this.setData({
              list: list
            })
          }
          if (res.page && res.page.records.length < 10) {
            this.setData({ isLiveEnd: true })
          }
          if (!res.page || !res.page.records.length) {
            this.setData({ isNoLiveData: true })
          }
        }).catch(err => {
          this.setData({ loading: false })
        })
      }
    },
    nextPage() {
      this.setData({
        livePager: {
          page: this.data.livePager.page + 1,
          pageSize: 10
        }
      });
      this.search();
    },
    resetData() {
      this.setData({
        list: [],
        isLiveEnd: false,
        isNoLiveData: false,
      })
      this.search()
    },
    
    getMenus(id) {
      const _this = this
      // 获取菜单列表
      this.getCate(id).then(res => {
        if (res.categoryVoList.length) {
          res.categoryVoList.forEach(cate => {
            cate.text = cate.itemName;
            cate.id = cate.cateId;
          })
          _this.setData({
            menus: res.categoryVoList,
          })
          // 获取二级菜单标签
          _this.getCate(res.categoryVoList[0].cateId).then(subRes => {
            if (subRes.categoryVoList && subRes.categoryVoList.length) {
              subRes.categoryVoList.forEach((cate, index) => {
                cate.text = cate.itemName;
                cate.id = cate.cateId;
                cate.isChecked = false
              })
              _this.setData({
                options: subRes.categoryVoList
              })
            }
          })
        }
      })
    },
    selectMenu(e) {
      const _this = this
      this.getCate(this.data.menus[e.detail.index].id).then(res => {
        if (res.categoryVoList && res.categoryVoList.length) {
          res.categoryVoList.forEach(cate => {
            cate.text = cate.itemName;
            cate.id = cate.cateId;
            cate.isChecked = false;
          })
          _this.setData({
            options: res.categoryVoList
          })
        }
      })
    },
    selectOption(e) {
      const _this = this
      const options = _this.data.options
      options.forEach(option => {
        option.isChecked = false
      })
      options[e.currentTarget.dataset.index].isChecked = true;
      _this.setData({
        options: options,
        showCondition: false
      })
      _this.sendParentParams(options[e.currentTarget.dataset.index])
    },
    sendParentParams(option) {
      this.setData({
        menuTitle: option.itemName,
        'filterMap.cateId': option.cateId,
        'searchCondition.filterMap.cateId': option.cateId
      })
      this.resetData()
    },
    getCate(id) {
      return new Promise(resolve => {
        findByPCateId({
          bizTypeEnum: 'WEB_CAST',
          pCateId: id === 'undefind' ? '' : id
        }).then(res => {
          resolve(res)
        })
      })
    },
    selectCondition(e) {
      this.setData({
        showCondition: true
      })
    },
    cancelSelectCondition(e) {
      this.setData({
        showCondition: false
      })
    },
    toggleSelect() {
      if (this.data.showCondition) {
        this.cancelSelectCondition()
      } else {
        this.selectCondition()
      }
    },
    bindScrollToLower() {
      if (!isLiveEnd) {
        this.nextPage()
      }
    }
  }
})
